package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.OaKeyworkTaskDTO;
import com.jcfk.system.domain.query.OaKeyworkTaskQuery;
import com.jcfk.system.service.OaKeyworkTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jcfk.system.utils.CheckSeqUtils.checkSeq;

/**
 * 重点工作任务
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-03-26
 */
@Api(tags = {"重点工作任务管理"})
@RestController
@RequestMapping("oakeyworktask")
public class OaKeyworkTaskController {
    @Autowired
    private OaKeyworkTaskService oaKeyworkTaskService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<OaKeyworkTaskQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询重点工作任务")
    @PostMapping("/queryByPage")
    public RetPage<OaKeyworkTaskDTO> queryByPage(@RequestBody ReqPageParam<OaKeyworkTaskQuery> requestParam) {
        DataPage<OaKeyworkTaskDTO> page = new DataPage<OaKeyworkTaskDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), OaKeyworkTaskDTO.class);
        page = oaKeyworkTaskService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("查询重点工作任务树结构")
    @PostMapping("/tree")
    public RetResult<List<OaKeyworkTaskDTO>> tree(@RequestBody OaKeyworkTaskQuery query) {
        return RetResult.ok(oaKeyworkTaskService.tree(query));
    }

    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("查询重点工作任务树结构无权限check")
    @PostMapping("/treeAll")
    public RetResult<List<OaKeyworkTaskDTO>> treeNotPimss(@Valid @RequestBody OaKeyworkTaskQuery query) {
        return RetResult.ok(oaKeyworkTaskService.treeNotPimss(query));
    }


    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("查询重点工作任务执行树结构")
    @PostMapping("/performance")
    public RetResult<List<OaKeyworkTaskDTO>> performance(@RequestBody OaKeyworkTaskQuery query) {
        return RetResult.ok(oaKeyworkTaskService.performance(query));
    }


    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("查询重点工作任务执行树结构")
    @PostMapping("/performanceAll")
    public RetResult<List<OaKeyworkTaskDTO>> performanceNotPimss(@RequestBody OaKeyworkTaskQuery query) {
        return RetResult.ok(oaKeyworkTaskService.performanceNotPimss(query));
    }


    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("查询当前用户的任务")
    @PostMapping("/user/tree")
    public RetResult<List<OaKeyworkTaskDTO>> userTree(@RequestBody OaKeyworkTaskQuery query) {
        return RetResult.ok(oaKeyworkTaskService.userTree(query));
    }


    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("根据id查询下面的子节点（单层）")
    @PostMapping("/sons/{id}")
    public RetResult<List<OaKeyworkTaskDTO>> sons(@PathVariable("id") String id) {
        return RetResult.ok(oaKeyworkTaskService.sons(id));
    }

    /**
     * 查询重点工作任务树结构
     */
    @ApiOperation("查询当前用户的任务")
    @PostMapping("/bulletinBoard")
    public RetResult<?> bulletinBoard(@RequestBody OaKeyworkTaskQuery query) {
        return RetResult.ok(oaKeyworkTaskService.bulletinBoard(query));
    }

    /**
     * 重点工作任务
     * 根据id 查询详情
     */
    @ApiOperation("查询重点工作任务")
    @GetMapping("/get/{id}")
    public RetResult<OaKeyworkTaskDTO> get(@PathVariable("id") String id) {
        OaKeyworkTaskDTO item = oaKeyworkTaskService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理重点工作任务
     */
    @ApiOperation("保存重点工作任务")
    @PostMapping("/save")
    @RequestLogHeader(modular = "重点工作任务管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid OaKeyworkTaskDTO oaKeyworkTask) {
        // 数据校验
        RetResult ret = oaKeyworkTaskService.isValid(oaKeyworkTask);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        checkSeq(oaKeyworkTask.getSeq());

        if (StrUtil.isEmpty(oaKeyworkTask.getId())) {
            // 新增
            oaKeyworkTaskService.insert(oaKeyworkTask);
        } else {
            // 更新
            oaKeyworkTaskService.update(oaKeyworkTask);
        }
        return RetResult.ok();

    }

    /**
     * 添加/更新管理重点工作任务
     */
    @ApiOperation("保存重点工作任务")
    @PostMapping("/update")
    @RequestLogHeader(modular = "重点工作任务管理", businessType = "保存")
    public RetResult<?> update(@RequestBody @Valid OaKeyworkTaskDTO oaKeyworkTask) {
        // 数据校验
        RetResult ret = oaKeyworkTaskService.isValid(oaKeyworkTask);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        checkSeq(oaKeyworkTask.getSeq());

        oaKeyworkTaskService.updateTask(oaKeyworkTask);
        return RetResult.ok();

    }


    /**
     * 删除重点工作任务
     */
    @ApiOperation("批量删除重点工作任务")
    @PostMapping("/delete/{id}")
    @RequestLogHeader(modular = "重点工作任务管理", businessType = "批量删除")
    public RetResult<?> delete(@PathVariable("id") String id) {
        oaKeyworkTaskService.delete(id);
        return RetResult.ok();
    }


    /**
     * 添加/更新管理重点工作任务
     */
    @ApiOperation("修改计划")
    @PostMapping("/updatePlan")
    @RequestLogHeader(modular = "重点工作任务管理", businessType = "保存")
    public RetResult<?> updatePlan(@RequestBody @Valid OaKeyworkTaskDTO oaKeyworkTask) {

        // 新增
        oaKeyworkTaskService.updataPlan(oaKeyworkTask);
        return RetResult.ok();
    }


    /**
     * 添加/更新管理重点工作任务
     */
    @ApiOperation("修改任务")
    @PostMapping("/updataGrossProfits")
    @RequestLogHeader(modular = "重点工作任务管理", businessType = "保存")
    public RetResult<?> updataGrossProfits(@RequestBody @Valid OaKeyworkTaskDTO oaKeyworkTask) {

        // 新增
        oaKeyworkTaskService.updataGrossProfits(oaKeyworkTask);
        return RetResult.ok();
    }

}
